package top.chukongxiang.monitorservice.javafx;

import cn.hutool.core.img.ImgUtil;
import javafx.application.Platform;
import lombok.extern.slf4j.Slf4j;
import top.chukongxiang.monitorservice.MonitorServiceApplication;
import top.chukongxiang.monitorservice.config.ApplicationStatusContextHolder;
import top.chukongxiang.monitorservice.javafx.fxml.ServersGui;
import top.chukongxiang.monitorservice.javafx.fxml.SettingGui;

import java.awt.*;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * 系统托盘
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/19 13:01
 */
@Slf4j
public class MonitorSystemTray {

    private static boolean isShow = false;

    private MonitorSystemTray() {}

    private static final TrayIcon trayIcon = initTrayIcon("imgs/logo.png");
    static {
        changeStatus();
    }

    public static void show() throws AWTException {
        if (isShow || trayIcon == null) {
            return;
        }
        //系统托盘
        SystemTray tray = SystemTray.getSystemTray();
        tray.add(trayIcon);
        isShow = true;
    }

    private static TrayIcon initTrayIcon(String imageUri) {
        if (!SystemTray.isSupported()) {
            //系统托盘不支持
            log.info(Thread.currentThread().getStackTrace()[1].getClassName() + ":系统托盘不支持");
            return null;
        }
        //此处不能选择ico格式的图片,要使用16*16的png格式的图片
        URL url = MonitorSystemTray.class.getClassLoader().getResource(imageUri);
        Image image = ImgUtil.getImage(url);
        //系统托盘图标
        TrayIcon trayIcon = new TrayIcon(image);
        //鼠标移到系统托盘,会显示提示文本
        trayIcon.setToolTip("监控");
        PopupMenu popupMenu = initPopupMenu();
        trayIcon.setPopupMenu(popupMenu);
        return trayIcon;
    }

    private static PopupMenu initPopupMenu() {
        PopupMenu popupMenu = new PopupMenu();
        popupMenu.add(initOptionMenuItem());
        popupMenu.addSeparator();
        popupMenu.add(initStorageServerMenuItem());
        popupMenu.add(initSettingMenuItem());
        popupMenu.add(initExitMenuItem());
        return popupMenu;
    }

    private static MenuItem initSettingMenuItem() {
        MenuItem menuItem = new MenuItem("设置");
        menuItem.addActionListener(e -> SettingGui.show());
        return menuItem;
    }

    private static MenuItem initStorageServerMenuItem() {
        MenuItem menuItem = new MenuItem("终端/存储配置");
        menuItem.addActionListener(e -> ServersGui.show());
        return menuItem;
    }

    private static MenuItem initExitMenuItem() {
        MenuItem menuItem = new MenuItem("退出");
        menuItem.addActionListener(e -> Platform.runLater(MonitorServiceApplication::close));
        return menuItem;
    }

    private static MenuItem initOptionMenuItem() {
        MenuItem menuItem = new MenuItem("立即启动");
        menuItem.setEnabled(false);
        menuItem.addActionListener(e -> Platform.runLater(MonitorServiceApplication::startSpring));
        return menuItem;
    }

    private static void changeStatus() {
        ApplicationStatusContextHolder.registerListener((observableValue, oldValue, newValue) -> {
            if (trayIcon == null) {
                return;
            }
            MenuItem menuItem = trayIcon.getPopupMenu().getItem(0);
            ActionListener[] actionListeners = menuItem.getActionListeners();
            for (ActionListener actionListener : actionListeners) {
                menuItem.removeActionListener(actionListener);
            }
            switch (newValue) {
                case STOPPED: {
                    menuItem.setLabel("立即启动");
                    menuItem.setEnabled(true);
                    menuItem.addActionListener(e -> Platform.runLater(MonitorServiceApplication::startSpring));
                    break;
                }
                case STARTING: {
                    menuItem.setLabel("启动中");
                    menuItem.setEnabled(false);
                    break;
                }
                case RUNNING: {
                    menuItem.setLabel("立即停止");
                    menuItem.setEnabled(true);
                    menuItem.addActionListener(e -> Platform.runLater(MonitorServiceApplication::stopSpring));
                    break;
                }
                case STOPPING: {
                    menuItem.setLabel("停止中");
                    menuItem.setEnabled(false);
                    break;
                }
                default: {
                    throw new RuntimeException("未知的应用状态：" + newValue);
                }
            }
        });
        ApplicationStatusContextHolder.setState(ApplicationStatusContextHolder.getCurrentStatus());
    }
}
